﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using TierService;
using OrderMgr.SysSetting;
using TierApplication;

namespace OrderMgr.SysSetting
{
    public partial class frmProductList : TierService.frmTemplate
    {
        public Utility.OrderType _OrderType = Utility.OrderType.None;
        public string _DepotID = "";

        public string _ProductID;

        public frmProductList()
        {
            InitializeComponent();
        }

        private void initForm()
        {
            Utility.DataToLookupComboxEx(txtDepotID, C_DMB.GetListByType(C_DMB.DType.DepotType.ToString()), "CodeID", "CodeName");
            Utility.DataToLookupComboxEx(txtProductType, C_DMB.GetListByType(C_DMB.DType.ProductType.ToString()), "CodeID", "CodeName");
            if (_DepotID != "")
            {
                ckbDepotID.Checked = true;
                txtDepotID.EditValue = _DepotID;
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            BindProductType("");
        }

        private void BindProductType(string sWhere)
        {
            string ls_where = "";

            ls_where = ls_where + sWhere;

            if ((txtProductID.Text.Trim() != "") && (ckbProductID.Checked))
                ls_where = ls_where + " and ProductID  like '%" + txtProductID.Text + "%'";


            if ((txtProductName.Text.Trim() != "") && (ckbProductName.Checked))
                ls_where = ls_where + " and ProductName  like '%" + txtProductName.Text + "%'";

            if ((txtProductType.Text.Trim() != "") && (ckbProductType.Checked))
                ls_where = ls_where + " and ProductType = '" + (string)txtProductType.EditValue + "'";

            string sDeportID = "";
            if ((txtDepotID.Text.Trim() != "") && (ckbDepotID.Checked))
                sDeportID = (string)txtDepotID.EditValue;
            TierService.WaitDialog.CreateWaitDialog("正在查询,请稍候","");
            T_Product mProduct = new T_Product();
            DataSet dsProduct = mProduct.GetProductList(ls_where, sDeportID);
            gcCustomer.DataSource = dsProduct.Tables[0];
            gvCustomer.ExpandAllGroups();
            TierService.WaitDialog.CloseWaitDialog();
        }

        private void btnExportExcel_Click(object sender, EventArgs e)
        {
            TierService.Utility.DataExportToExcel(gvCustomer, "商品信息");
        }


        private void gvCustomer_DoubleClick(object sender, EventArgs e)
        {

            if (gvCustomer.RowCount < 1) return;
            DataRow dr = gvCustomer.GetDataRow(gvCustomer.FocusedRowHandle);
            decimal EndQuantity = decimal.Parse(dr["EndQuantity"].ToString());

            if ((_OrderType == Utility.OrderType.Sale))
            {
                if (EndQuantity <= 0)
                {
                    if (TierService.Utility.Msg_Question("当前商品库存不足,是否仍要销售？") != System.Windows.Forms.DialogResult.OK)
                    {
                        return;
                    }
                    //MessageBox.("当前商品库存不足,不能选择");
                    //return;
                }
            }
            _ProductID = dr["ProductID"].ToString();
            DialogResult = System.Windows.Forms.DialogResult.OK;

        }


        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (gvCustomer.RowCount < 1) return;
            if (MessageBox.Show("是否要删除此客户记录，删除后将无法恢复!", "确认", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
                return;
            DataRow dr = gvCustomer.GetDataRow(gvCustomer.FocusedRowHandle);
            string sID = dr["ProductID"].ToString();

            T_Customer mCustomer = new T_Customer();
            mCustomer.Delete(sID);

            btnSearch_Click(null, null);
        }


        private void gvCustomer_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            e.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Far;
            if (e.Info.IsRowIndicator)
            {
                if (e.RowHandle >= 0)
                {
                    e.Info.DisplayText = (e.RowHandle + 1).ToString();
                }
                else if (e.RowHandle < 0 && e.RowHandle > -1000)
                {
                    e.Info.Appearance.BackColor = System.Drawing.Color.AntiqueWhite;
                    e.Info.DisplayText = "G" + e.RowHandle.ToString();
                }
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (gProductType == Utility.ProductType.MProduct)
            {
                frmProductDetail fDetail = new frmProductDetail();
                fDetail.OpMode = TierService.Utility.OperateMode.Insert;
                fDetail.gProductType = Utility.ProductType.MProduct;
                fDetail.ShowDialog();

                btnSearch_Click(null, null);
            }

            if (gProductType == Utility.ProductType.Product)
            {
                frmProductDetail fDetail = new frmProductDetail();
                fDetail.OpMode = TierService.Utility.OperateMode.Insert;
                fDetail.gProductType = Utility.ProductType.Product;
                fDetail.ShowDialog();

                btnSearch_Click(null, null);
            }
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            if (gvCustomer.RowCount < 1) return;
            DataRow dr = gvCustomer.GetDataRow(gvCustomer.FocusedRowHandle);
            string sID = dr["ProductID"].ToString();
            string sProductType = dr["ProductProp"].ToString();

            frmProductDetail fDetail = new frmProductDetail();
            fDetail.OpMode = TierService.Utility.OperateMode.Update;
            if (sProductType == "1")
                fDetail.gProductType = Utility.ProductType.MProduct;
            else
                fDetail.gProductType = Utility.ProductType.Product;
            fDetail._ProductID = sID;
            if (fDetail.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                btnSearch_Click(null, null);
            }
        }

        private void frmProductList_Load(object sender, EventArgs e)
        {
            initForm();
        }

        private void gvCustomer_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            //if (gvCustomer.RowCount < 1) return;
            //DataRow dr = gvCustomer.GetDataRow(gvCustomer.FocusedRowHandle);
            ////dr["IsChecked"] = (dr["IsChecked"] == "1") ? "0" : "1";
        }



        private int GetSelectRowCount()
        {
            int iCount = 0;
            for (int i = 0; i < gvCustomer.DataRowCount; i++)
            {
                DataRow dr = gvCustomer.GetDataRow(i);
                if (dr["IsChecked"] == "1")
                {
                    iCount = iCount + 1;
                }
            }
            return iCount;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            if (gvCustomer.RowCount < 1) return;
            DataRow dr = gvCustomer.GetDataRow(gvCustomer.FocusedRowHandle);
            string sID = dr["ProductID"].ToString();
            string sProductType = dr["ProductProp"].ToString();

            frmProductDetail fDetail = new frmProductDetail();
            fDetail.OpMode = TierService.Utility.OperateMode.Query;
            if (sProductType == "1")
                fDetail.gProductType = Utility.ProductType.MProduct;
            else
                fDetail.gProductType = Utility.ProductType.Product;

            fDetail._ProductID = sID;
            fDetail.ShowDialog();
        }

        private void gvProductType_DoubleClick(object sender, EventArgs e)
        {
            //if (gvProductType.RowCount < 1) return;
            //DataRow dr = gvProductType.GetDataRow(gvProductType.FocusedRowHandle);
            //string sProductType = dr["ProductTypeID"].ToString();
            ////string sProductPropID = dr["ProductPropID"].ToString();

            //string sWhere = " and ProductType='" + sProductType + "'";
            //TierService.WaitDialog.CreateWaitDialog("正在查询，请等待", "正在查询，请等待");
            //BindProductType(sWhere);
            //TierService.WaitDialog.CloseWaitDialog();
        }

        private void rgProductProp_EditValueChanged(object sender, EventArgs e)
        {
            //string sProductProp = (string)rgProductProp.EditValue;

            //if (sProductProp == "1")
            //{
            //    gcProductType.DataSource = T_Product.GetProductType("").Tables[0];
            //    gvProductType.ExpandAllGroups();

            //    //BindProductType("");
            //}



            //if (sProductProp == "1")
            //{
            //    gcProductType.DataSource = T_Product.GetProductType(" and Remark='1'").Tables[0];
            //    gvProductType.ExpandAllGroups();

            //    // BindProductType(" and ProductProp='1'");
            //}
            //if (sProductProp == "2")
            //{
            //    gcProductType.DataSource = T_Product.GetProductType(" and Remark='2'").Tables[0];
            //    gvProductType.ExpandAllGroups();

            //    //BindProductType(" and ProductProp='2'");
            //}

        }

        private void gvCustomer_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
        {
            if (e.RowHandle >= 0)
            {
                DataRow dr = gvCustomer.GetDataRow(e.RowHandle);
                decimal dDepotUp = 0;
                decimal dDepotDown = 0;
                decimal dEndQuantity = 0;

                try
                {
                    dDepotUp = decimal.Parse(dr["DepotUp"].ToString());
                }
                catch (Exception)
                {
                    dDepotUp = 0;
                }

                try
                {
                    dDepotDown = decimal.Parse(dr["DepotDown"].ToString());
                }
                catch (Exception)
                {
                    dDepotDown = 0;
                }

                try
                {
                    dEndQuantity = decimal.Parse(dr["EndQuantity"].ToString());
                }
                catch (Exception)
                {
                    dEndQuantity = 0;
                }


                if (dEndQuantity < dDepotDown)
                {
                    e.Appearance.ForeColor = Color.Silver;
                }
                if (dEndQuantity > dDepotUp)
                {
                    e.Appearance.ForeColor = Color.Green;
                }

                if (dEndQuantity <= 0)
                {
                    e.Appearance.ForeColor = Color.Red;
                }
            }
        }


    }
}